package com.powernode.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.ProdComm;
import com.powernode.model.Result;
import com.powernode.service.ProdCommService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author RenBoQing
 * @date 2024年07月28日 10:41
 * @Description 商品评论控制器
 */
@Api(tags = "商品评论接口管理")
@RestController
@RequestMapping("product/prodComm")
public class ProdCommController {
    @Autowired
    private ProdCommService prodCommService;
     /*
      * 多条件分页查询商品评论
      * @author RenBoQing
      * @date 2024/7/28 11:02
      * @param current
      * @param size
      * @param proName
      * @param status
      * @return com.powernode.model.Result<com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.powernode.domain.ProdComm>>
      */
    @ApiOperation("多条件分页查询商品评论")
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('prod:prodComm:page')")
    public Result<Page<ProdComm>> loadProductCommPage(@RequestParam(defaultValue = "1") Long current,
                                                      @RequestParam(defaultValue = "10") Long size,
                                                      @RequestParam(required = false) String proName,
                                                      @RequestParam(required = false) Integer status) {
        //构建 分页对象
        Page<ProdComm> prodCommPage = new Page<>(current, size);
        // 查询 多条件分页查询
        prodCommPage = prodCommService.page(prodCommPage, new LambdaQueryWrapper<ProdComm>().eq(ObjectUtil.isNotNull(status), ProdComm::getStatus, status).like(StringUtils.hasText(proName), ProdComm::getProdName, proName).orderByDesc(ProdComm::getCreateTime));
        // 返回查询的数据
        return Result.success(prodCommPage);
    }

    @ApiOperation("根据商品Id查询评论详情")
    @GetMapping("/id")
    @PreAuthorize("hasAuthority('prod:prodComm:info')")
    public Result<ProdComm> getProductCommById(@PathVariable Long id) {
        ProdComm prodComm = prodCommService.getById(id);
        return Result.success(prodComm);
    }
    @ApiOperation("回复和审核商品的评论")
    @PutMapping("")
    @PreAuthorize("hasAuthority('prod:prodComm:update')")
    public Result<String> updateProductComm(@RequestBody ProdComm productComm) {
        boolean update = prodCommService.replyAndExamProdComm(productComm);
        return Result.handle(update);
    }
}
